package com.asset.dispoal.user.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.user.domain.db.entity.SysUserDo;
import com.asset.dispoal.user.domain.db.mapper.SysUserMapper;
import com.asset.dispoal.user.dto.UserRequest;
import com.asset.dispoal.user.dto.UserResponse;
import com.asset.dispoal.user.service.converter.UserConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/7$
 */
@Component
public class UserQueryPageCommand implements BaseCommand<UserRequest, PageVO<UserResponse>> {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public PageVO<UserResponse> execute(UserRequest userRequest) {
        final SysUserDo sysUserDo = UserConverter.INSTANCE.reqToDo(userRequest);
        sysUserDo.setPermission((String) GlobalHeaderThreadLocal.get().getExtraMap().get("permission"));
        final int i = sysUserMapper.pageCount(sysUserDo);
        final Page<SysUserDo> page = PagingQuery.getPage(i, sysUserDo);
        PageVO<UserResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(i);
        if (page != null) {
            final List<SysUserDo> userDos = sysUserMapper.page(page);
            final List<UserResponse> userResponses = UserConverter.INSTANCE.doToRes(userDos);
            pageVO.setResult(userResponses);
        }
        return pageVO;
    }


}
